package com.excel.controler;

import com.excel.entity.ExcelTable;
import com.excel.entity.ExcelTableTest;
import com.excel.poi.sax.AnalysisEventListener;
import com.excel.poi.sax.inter.AnalysisContextImpl;
import com.excel.poi.pojo.ExcelExportParam;
import com.excel.poi.pojo.ExcelImportVo;
import com.excel.poi.util.ExcelUtil;
import com.excel.poi.util.ExcelUtilSax;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 赵玮
 * @Description lb
 * @create /2022/3/14
 */
@RestController
@RequestMapping("ControllerPoi")
public class ControllerPoi {


    @Autowired
    com.excel.dao.ExcelTableMapper excelTableMapper;


    /**
     * sax方式导入
     * @author 赵玮
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/importExcelBySax",method = RequestMethod.POST)
    public   List  importExcelBySax(MultipartFile file) throws Exception {
        List<ExcelTable> objects = new ArrayList<>();
        ExcelUtilSax.readBySax(file.getInputStream(),ExcelTable.class,new AnalysisEventListener<ExcelTable>() {
            @Override
            public void invoke(ExcelTable object, AnalysisContextImpl context) {
                System.out.println(object + "===" + context);
                objects.add(object);
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContextImpl context) {
                System.out.println(context);
            }
        });
        return objects;
    }
    /**
     * 导入 ExcelTable属性为例  50万+
     * @author 赵玮
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/importExcel",method = RequestMethod.POST)
    public   List  importExcel(MultipartFile file) throws Exception {
        ExcelImportVo excelImportVo = new ExcelImportVo();
        excelImportVo.setPojoClass(ExcelTable.class);
        excelImportVo.setTitleRows(1);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name","名字");
        excelImportVo.setPlaceholderMap(hashMap);
        return ExcelUtil.readExcel(file.getInputStream(),excelImportVo);
    }

    @GetMapping(value = "/downLoad")
    public  void  downLoad(HttpServletResponse response) throws Exception {
        List<ExcelTable> excelTables = excelTableMapper.selectAll();
        ExcelExportParam excelExportParam = new ExcelExportParam();
        excelExportParam.setFileName("测试");
        excelExportParam.setDataList(excelTables);
        excelExportParam.setSourceClass(ExcelTable.class);

        //报表下载
        ExcelUtil.exportExcel(response, excelExportParam);
    }

    @GetMapping(value = "/downLoad2")
    public  void  downLoad2(HttpServletResponse response) throws Exception {
        List<ExcelTable> excelTables = excelTableMapper.selectAll();
        ExcelExportParam excelExportParam = new ExcelExportParam();
        excelExportParam.setTitleName("我是标题");
        excelExportParam.setFileName("测试");
        excelExportParam.setSheetName("测试sheet");
        //长文本换行
        excelExportParam.setLineFeed(true);
        excelExportParam.setDataList(excelTables);
       // excelExportParam.setDataList(new ArrayList<>());
        excelExportParam.setSourceClass(ExcelTable.class);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name","名字");
        excelExportParam.setPlaceholderMap(hashMap);

        //报表下载
        ExcelUtil.exportExcel(response, excelExportParam);
    }

    @GetMapping(value = "/downLoad3")
    public  void  downLoad3(HttpServletResponse response) throws Exception {
       List<ExcelExportParam> list = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            List<ExcelTable> excelTables = excelTableMapper.selectAll();
            ExcelExportParam excelExportParam = new ExcelExportParam();
            excelExportParam.setTitleName("我是标题");
            excelExportParam.setFileName("测试");
            excelExportParam.setSheetName(i+"测试sheet");
            //长文本换行
            excelExportParam.setLineFeed(true);
            excelExportParam.setDataList(excelTables);
            excelExportParam.setSourceClass(ExcelTable.class);
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("name","名字");
            hashMap.put("peopleInfo","人员信息");
            excelExportParam.setPlaceholderMap(hashMap);
            excelExportParam.setTitleForegroundColor(IndexedColors.GREY_50_PERCENT);
            excelExportParam.setHeadForegroundColor(IndexedColors.GREY_50_PERCENT);
            list.add(excelExportParam);
        }
        //报表下载
        ExcelUtil.exportExcelMoreSheet(response, list);
    }
    @GetMapping(value = "/downLoad4")
    public  void  downLoad4(HttpServletResponse response) throws Exception {
        List<ExcelExportParam> list = new ArrayList<>();
        List<ExcelTable> excelTables = excelTableMapper.selectAll();
        ExcelExportParam param1 = new ExcelExportParam();
        param1.setTitleName("我是标题");
        param1.setFileName("测试");
        param1.setSheetName("测试sheet");
        //长文本换行
        param1.setLineFeed(true);
        param1.setDataList(excelTables);
        param1.setSourceClass(ExcelTable.class);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", "名字");
        hashMap.put("peopleInfo", "人员信息");
        param1.setPlaceholderMap(hashMap);
        list.add(param1);

       List<ExcelTableTest> list2 = new ArrayList<>();
        ExcelTableTest excelTableTest = new ExcelTableTest();
        excelTableTest.setName("测试名字");
        list2.add(excelTableTest);
        ExcelExportParam param2 = new ExcelExportParam();
        param2.setTitleName("我是标题");
        param2.setFileName("测试");
        param2.setSheetName("测试sheet2");
        //长文本换行
        param2.setLineFeed(true);
        param2.setDataList(list2);
        param2.setSourceClass(ExcelTableTest.class);
        param2.setPlaceholderMap(hashMap);
        list.add(param2);


        //报表下载
        ExcelUtil.exportExcelMoreSheet(response, list);
    }
}
